import {compressAccurately} from "image-conversion";
import {ElMessage} from "element-plus";
import {Address} from '../assets/province_city_area.json';
function deepClone(obj) {
    var target = {};
    for (var key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
            if (typeof obj[key] === 'object') {
                target[key] = deepClone(obj[key]);
            } else {
                target[key] = obj[key];
            }
        }
    }
    return target;
}
function getAllDatesInRange(startDate, endDate) {
    let dates = [];
    let datesValue = [];
    let currentDate = new Date(startDate);

    while (currentDate <= endDate) {
        dates.push(new Date(currentDate));
        currentDate.setDate(currentDate.getDate() + 1);
    }
    console.log()
    dates.forEach(date => {
        let value = date.toISOString().split('T')[0];
        datesValue.push(value);
    });
    return datesValue;
}
export const fieldList = [
    {
        type: '文本',
        config:{
            type:'input',
            field:'',
            title:'',
            value:'',
            props:{
                placeholder:'请输入',
            },
            effect: {
                required: true
            }
        }
    },
    {
        type: '单选',
        config:{
            type:'radio',
            field:'',
            title:'',
            value:'',
            options: [],
            effect: {
                required: true
            }
        }
    },
    {
        type: '三级选择',
        config: {
            type: "cascader",
            title: "",
            field: "",
            value: '',
            props: {
                placeholder:'请选择',
                options:Address,
                props:{
                    value:'label'
                }
            },
            effect: {
                required: true
            }
        }
    },
    {
        type: '下拉选择',
        config:{
            type: "select",
            field: "",
            title: "",
            value: '',
            options: [],
            props: {
                valueKey:'label',
                placeholder:"请选择",
            },
            effect: {
                required: true
            }
        }
    },
    {
        type: '日期选择',
        config: {
            type: "DatePicker",
            field: "",
            title: "",
            value: '',
            props: {
                type: "date",
                format: "YYYY/MM/DD",
                placeholder:"请选择日期",
            },
            effect: {
                required: true
            }
        }
    },
    {
        type: '时间范围选择',
        config: {
            type: "TimePicker",
            field: "",
            title: "",
            value: '',
            props: {
                isRange:true,
            },
            effect: {
                required: true
            }
        }
    },
    {
        type: '范围日期选择',
        config: {
            type: "DatePickers",
            field: "",
            title: "",
            value: [],
            props: {
                type: "daterange",
                format: "YYYY-MM-DD",
                placeholder:"请选择日期",
                rangeSeparator:'至',
                valueFormat:"YYYY-MM-DD"
            },
            effect: {
                required: true
            }
        }
    },
    {
        type: '范围时间组选择',
        config: {
            type: 'group',
            field: '',
            title: '',
            value: [],
            props: {
                rule: [
                    {
                        type: 'TimePicker',
                        field: 'field1',
                        title: '时间段',
                        props: {
                            isRange: true,
                            valueFormat:"HH-mm-ss"
                        },
                    },
                ],
                field: 'field1',
                button: false,
                sortBtn: false
            },
            effect: {
                required: true
            }
        }
    },
    {
        type: '图片上传',
        config: {
            type: "upload",
            field: "",
            title: "",
            value: [],
            effect: {
                required: true
            },
            props: {
                type:"select",
                uploadType:"image",
                action: "https://api.uexpo.com/partyapi/party/oss/filePartETag",
                name:"file",
                multiple: false,
                accept:"image\/*",
                limit: 1,
                autoUpload:true,
                onSuccess(res,file) {
                    console.log(res);
                    console.log(file);
                    file.url = res.data;
                },
                beforeUpload(file) {
                    console.log(file);
                    return new Promise((resolve,reject) => {
                        if(file.size / 1024 > 500) {
                            compressAccurately(file,500)
                                .then(res => {
                                    console.log(res)
                                    resolve(res);
                                })
                        }else{
                            resolve(file.raw)
                        }
                    })
                },
                onError(res) {
                    ElMessage({message: '上传错误', type: 'warning' });
                }
            },
        }
    }
]

// 添加字段
export const addField = (type,params,options,isShow) => {
    let { field,title,value } = params;
    let config = {}
    switch (type) {
        case 'input':{
            config = {
                type:'input',
                field:'',
                title:'',
                value:'',
                props:{
                    placeholder:'请输入',
                },
                effect: {
                    required: true
                }
            }
            break ;
        }
        case 'radio':{
            config = {
                type:'radio',
                field:'',
                title:'',
                value:'',
                options: [],
                effect: {
                    required: true
                }
            }
            break ;
        }
        case 'cascader':{
            config = {
                type: "cascader",
                title: "",
                field: "",
                value: '',
                props: {
                    placeholder:'请选择',
                    options:Address,
                    props:{
                        value:'label'
                    }
                },
                effect: {
                    required: true
                }
            }
            break ;
        }
        case 'select':{
            config = {
                type: "select",
                field: "",
                title: "",
                value: '',
                options: [],
                props: {
                    valueKey:'label',
                    placeholder:"请选择",
                },
                effect: {
                    required: true
                }
            }
            break ;
        }
        case 'DatePicker':{
            config = {
                type: "DatePicker",
                field: "",
                title: "",
                value: '',
                props: {
                    type: "date",
                    format: "YYYY/MM/DD",
                    placeholder:"请选择日期",
                    valueFormat:"YYYY-MM-DD"
                },
                effect: {
                    required: true
                }
            }
            break ;
        }
        case 'TimePicker':{
            config = {
                type: "TimePicker",
                field: "",
                title: "",
                value: ['00:00:00','23:59:59'],
                props: {
                    isRange:true,
                    valueFormat:'HH:mm:ss'
                },
                effect: {
                    required: true
                }
            }
            break ;
        }
        case 'DatePickers':{
            config = {
                type: "DatePicker",
                field: "",
                title: "",
                value: [],
                props: {
                    type: "dates",
                    format: "YYYY-MM-DD",
                    startPlaceholder:"请选择开始日期",
                    endPlaceholder:"请选择结束日期",
                    valueFormat:"YYYY-MM-DD"
                },
                effect: {
                    required: true
                },
                update(val, rule, api) {
                    let times = api.getRule('times');
                    console.log(val);
                    console.log(times);
                    if(val) {
                        let ruleArr = [];
                        let objkey = {};
                        val.forEach((v,i) => {
                            ruleArr.push({
                                type: 'TimePicker',
                                title: v,
                                field:v,
                                props:{
                                    isRange:true,
                                    valueFormat:'HH:mm:ss'
                                }
                            })
                            if(!isShow) {
                                objkey[v] = ['00:00:00','23:59:59']
                            }
                        })
                        times.props.rule = ruleArr;
                        times.value = [objkey];
                    }else {
                        times.props.rule = [];
                        times.value = [];
                    }
                }
            }
            break ;
        }
        case 'group':{
            config = {
                type: 'group',
                field: '',
                title: '',
                value: [],
                props: {
                    rule: [],
                    // field: 'field1',
                    button: false,
                    sortBtn: false
                },
                effect: {
                    required: true
                },
            }
            break ;
        }
        case 'upload':{
            config = {
                type: "upload",
                field: "",
                title: "",
                value: [],
                effect: {
                    required: true
                },
                props: {
                    type:"select",
                    uploadType:"image",
                    action: "https://api.uexpo.com/partyapi/party/oss/filePartETag",
                    name:"file",
                    multiple: false,
                    accept:"image\/*",
                    limit: 1,
                    autoUpload:true,
                    onSuccess(res,file) {
                        console.log(res);
                        console.log(file);
                        file.url = res.data;
                    },
                    beforeUpload(file) {
                        console.log(file);
                        return new Promise((resolve,reject) => {
                            if(file.size / 1024 > 500) {
                                compressAccurately(file,500)
                                    .then(res => {
                                        console.log(res)
                                        resolve(res);
                                    })
                            }else{
                                resolve(file.raw)
                            }
                        })
                    },
                    onError(res) {
                        ElMessage({message: '上传错误', type: 'warning' });
                    }
                },
            }
            break ;
        }
    }
    config.field = field;
    config.title = title;
    if(value) {
        config.value = value;
    }
    if(options) {
        config.options = options;
    }
    return config;
}
